package edu.ncsu.csc.ip.client.run;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import edu.ncsu.csc.ip.client.PeerClient;
import edu.ncsu.csc.ip.common.Log;

public class RunTask1 {

	public static void main(String[] args) {

		String[] testArray = {"152.1.247.155", "152.1.247.162",
				"65423", "test.txt", "500" };

		int numNodes = testArray.length - 3;
		long startTime = 0;
		long endTime = 0;
		long duration = 0;
		long avg = 0;
		
		while (numNodes > 0) {
			PeerClient pc = new PeerClient(testArray);
			Log.println("Starting test");
			try {
				File file = new File("./Task1_" + numNodes
						+ "_Nodes_results.csv");
				PrintWriter printWrite = new PrintWriter(new FileWriter(file,
						false));
				printWrite.println("Test# , Time in ms");
				for (int i = 0; i < 5; i++) {
					startTime = System.currentTimeMillis();
					pc.runClient();
					endTime = System.currentTimeMillis();
					duration = endTime - startTime;
					avg += duration;
					printWrite.println(i + "," + duration);
				}
				avg = avg / 5;
				printWrite.println("Average" + "," + avg);
				printWrite.println("Average in seconds" + "," + avg / 1000);
				printWrite.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			Log.println("End of test");
			numNodes--;
			avg = 0;
			testArray = Arrays.copyOfRange(testArray, 1, testArray.length);
		}
		System.exit(0);
	}
}
